package cn.itcast.wushangxiantu.Fragment.guozhanshenqin;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.FindAllGuoJiaAdapter;
import cn.itcast.wushangxiantu.UserData.GuoJiaData;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GuoJiaLieBiaoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GuoJiaLieBiaoFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Context context;
    private View view;
    private ViewHolder viewHolder;
    private FindAllGuoJiaAdapter adapter;
    private GuoJiaData guoJiaData;
    private Button btnShenQing;
    private int num = -1;

    public GuoJiaLieBiaoFragment(Context context) {
        this.context = context;
    }

    public GuoJiaLieBiaoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GuoJiaLieBiaoFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GuoJiaLieBiaoFragment newInstance(String param1, String param2) {
        GuoJiaLieBiaoFragment fragment = new GuoJiaLieBiaoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_guo_jia_lie_biao, container, false);
        viewHolder = new ViewHolder(view);
        return view;
    }

    class ViewHolder {
        public View rootView;
        public ListView lv;
        public TextView tvName;
        public TextView tvDj;
        public TextView tvGuoKu;
        public TextView tvNumber;
        public TextView tvTitle;
        public Button btnShenQing;
        public Button btnShuaXin;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.lv = (ListView) rootView.findViewById(R.id.lv);
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvDj = (TextView) rootView.findViewById(R.id.tvDj);
            this.tvGuoKu = (TextView) rootView.findViewById(R.id.tvGuoKu);
            this.tvNumber = (TextView) rootView.findViewById(R.id.tvNumber);
            this.tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
            this.btnShenQing = (Button) rootView.findViewById(R.id.btnShenQing);
            this.btnShuaXin = (Button) rootView.findViewById(R.id.btnShuaXin);
            findAllGuoJia();
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    num = i;
                    tvDj.setText("等级:" + guoJiaData.getGuoJiaYuLan().get(i).getDj());
                    tvName.setText("名字:" + guoJiaData.getGuoJiaYuLan().get(i).getName());
                    tvGuoKu.setText("战场收益:" + guoJiaData.getGuoJiaYuLan().get(i).getSy());
                    tvTitle.setText("公告:" + guoJiaData.getGuoJiaYuLan().get(i).getTitle());
                    String content = "人数:" + guoJiaData.getGuoJiaYuLan().get(i).getNumber() + "/";
                    int num = guoJiaData.getGuoJiaYuLan().get(i).getDj();
                    num /= 5;
                    if (num > 19) {
                        num = 19;
                    }
                    num++;
                    content += num;
                    tvNumber.setText(content);
                }
            });
            btnShenQing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (num==-1){
                        return;
                    }
                    GuoJiaShenQin(Login.getUser().getUsernumber(), Login.getUser().getPassword(), guoJiaData.getGuoJiaYuLan().get(num).getGjid());
                }
            });
            btnShuaXin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tvDj.setText("等级:" );
                    tvName.setText("名字:" );
                    tvGuoKu.setText("战场收益:" );
                    tvTitle.setText("公告:" );
                    tvNumber.setText("人数：");
                    findAllGuoJia();
                    num=-1;
                }
            });
        }

        private void GuoJiaShenQin(String usernumber, String password, int gjid) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=GuoJiaShenQin" + "&userNumber=" + usernumber + "&passWord=" + password + "&gj_id=" + gjid, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("guojiashenqing", e.getMessage());
                    } catch (Exception e1) {

                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Log.v("guojiashenqing", res);
                    User guoJiaData = new Gson().fromJson(res, User.class);
                    Message message = new Message();
                    message.obj = guoJiaData;
                    message.what = 2;
                    handler.sendMessage(message);
                }
            });
        }

        private void findAllGuoJia() {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=findAllGuoJia", new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("findAllGuoJia", e.getMessage());
                    } catch (Exception e1) {

                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Log.v("findAllGuoJia", res);
                    GuoJiaData guoJiaData = new Gson().fromJson(res, GuoJiaData.class);
                    Message message = new Message();
                    message.obj = guoJiaData;
                    message.what = 1;
                    handler.sendMessage(message);
                }
            });
        }

        Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                switch (message.what) {
                    case 1:
                        guoJiaData = (GuoJiaData) message.obj;
                        if (null != guoJiaData) {
                            if (adapter == null) {
                                adapter = new FindAllGuoJiaAdapter(context);
                            }
                            adapter.setGuoJiaData(guoJiaData);
                            lv.setAdapter(adapter);
                        } else {
                            ToastUtil.toast(context, "服务器炸了");
                        }
                        break;
                    case 2:
                        User user = (User) message.obj;
                        if (null != user) {
                            ToastUtil.toast(context, user.getStw());
                        } else {
                            ToastUtil.toast(context, "服务器炸了");
                        }
                        break;
                }
                return false;
            }
        });

    }
}